package user.view;



import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JPasswordField;

import user.model.UserModel;
import user.ucm.UCRegistering;

import db.UserValidate;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.JSeparator;

public class UserEditFrame extends JFrame {

	private JPanel contentPane;
	private JPasswordField passwordField;
	private JPasswordField confirmField;
	private JTextField address_field;
	private JTextField phone_text;
	private JLabel check_label;
	private JLabel warn_label;
	private JLabel res_label;
	private static UserEditFrame inst;
	private JSeparator separator;
	public static UserEditFrame getInstance()
	{
		if(inst==null)inst=new UserEditFrame();
		return inst;
	}
	/**
	 * Launch the application.
	 */


	/**
	 * Create the frame.
	 */
	private UserEditFrame() {
		setTitle("Edit");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 348);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		{
			check_label = new JLabel("");
			check_label.setForeground(Color.BLUE);
			check_label.setBounds(100, 25, 285, 15);
			contentPane.add(check_label);
		}
		{
			JLabel pwd = new JLabel("Password(*)");
			pwd.setBounds(10, 56, 80, 15);
			contentPane.add(pwd);
		}
		{
			JLabel lblConfirm = new JLabel("Confirm(*)");
			lblConfirm.setBounds(10, 101, 80, 15);
			contentPane.add(lblConfirm);
		}
		{
			passwordField = new JPasswordField();
			passwordField.setBounds(100, 53, 137, 21);
			contentPane.add(passwordField);
		}
		{
			confirmField = new JPasswordField();
			confirmField.setBounds(100, 98, 137, 21);
			contentPane.add(confirmField);
		}
		{
			JLabel lblAddress = new JLabel("Address");
			lblAddress.setBounds(10, 214, 54, 15);
			contentPane.add(lblAddress);
		}
		{
			address_field = new JTextField();
			address_field.setBounds(100, 211, 285, 21);
			contentPane.add(address_field);
			address_field.setColumns(10);
		}
		{
			JLabel lblPhone = new JLabel("phone");
			lblPhone.setBounds(10, 177, 54, 15);
			contentPane.add(lblPhone);
		}
		{
			phone_text = new JTextField();
			phone_text.setBounds(100, 174, 137, 21);
			contentPane.add(phone_text);
			phone_text.setColumns(10);
		}
		{
			warn_label = new JLabel("");
			warn_label.setForeground(Color.RED);
			warn_label.setBounds(247, 101, 177, 15);
			contentPane.add(warn_label);
		}
		{
			JButton btnRegister = new JButton("Update");
			btnRegister.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					
					String pwd=new String(passwordField.getPassword());
					if(pwd.trim().equals(""))
					{
						warn_label.setText("please input password!");
						return;
					}
					String pwd1=new String(confirmField.getPassword());
					if(!pwd.equals(pwd1))
					{
						warn_label.setText("wrong confirm password!");
						return;
					}
			
					UserModel um=new UserModel();
					um.setAddress(address_field.getText().trim());
					um.setPhone(phone_text.getText().trim());
					um.setType(0);
					um.setName(LoginView.getInstance().getUsername());
					um.setPsw(pwd);
					
					if(1==UserValidate.updateInfo(um))
					{
						passwordField.setText("");
						confirmField.setText("");
						warn_label.setText("");
						res_label.setText("update ok!");
					}else res_label.setText("system error!");
				}
			});
			btnRegister.setBounds(100, 265, 93, 23);
			contentPane.add(btnRegister);
			updateInfo();
		}
		{
			res_label = new JLabel("");
			res_label.setForeground(Color.RED);
			res_label.setBounds(214, 269, 210, 15);
			contentPane.add(res_label);
		}
		{
			separator = new JSeparator();
			separator.setBounds(10, 150, 378, 21);
			contentPane.add(separator);
		}
	}
	private void updateInfo()
	{
		Vector<String> vector=UserValidate.getUserInfo(LoginView.getInstance().getUsername(), 0);
		address_field.setText(vector.elementAt(0));
		phone_text.setText(vector.elementAt(1));
	}
}
